﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UtilZ.Tool.WinformTestApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            label2.Text = Process.GetCurrentProcess().Id.ToString();
            timer1.Start();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            int a = 10;
            int b = 0;
            int c = a / b;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            throw new Exception("xx");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            while (true)
            {

            }
        }




        private void button6_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                button1_Click(sender, e);
            });
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                button2_Click(sender, e);
            });
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                button3_Click(sender, e);
            });
        }



        private CancellationTokenSource _cts = null;
        private CancellationToken _token = CancellationToken.None;
        private int _interval = 1000;

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                this._interval = (int)(numericUpDown1.Value * 1000);
                if (this._cts != null)
                {
                    this._cts.Cancel();
                    this._cts.Dispose();
                    this._cts = null;
                }
                this._cts = new CancellationTokenSource();
                this._token = this._cts.Token;

                Task.Factory.StartNew(() =>
                {
                    using (UdpClient client = new UdpClient(5401))
                    {
                        IPEndPoint dstEndPoint = new IPEndPoint(IPAddress.Loopback, 5301);
                        byte[] dram = new byte[] { 100 };
                        while (!this._token.IsCancellationRequested)
                        {
                            client.Send(dram, dram.Length, dstEndPoint);
                            Thread.Sleep(this._interval);
                        }
                    }

                }, this._token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
            }
            else
            {
                if (this._cts != null)
                {
                    this._cts.Cancel();
                    this._cts.Dispose();
                    this._cts = null;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
        }
    }
}
